Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import { eq, and } from 'drizzle-orm' import { type NextRequest, NextResponse } from 'next/server' import { db } from '@/db' import { customSkills } from '@/db/schema' import { getUserId } from '@/lib/viewer' import { withAuth } from '@/lib/auth/withAuth' /** * PUT /api/worksheets/skills/custom/[id] * * Update an existing custom skill */ export const PUT = withAuth(async (request, { params }) => { try { const userId = await getUserId() const { id } = (await params) as { id: string } const body = await request.json() const { name, description, digitRange, regroupingConfig, displayRules } = body // Verify skill exists and belongs to user const existing = await db.query.customSkills.findFirst({ where: and(eq(customSkills.id, id), eq(customSkills.userId, userId)), }) if (!existing) { return NextResponse.json({ error: 'Custom skill not found' }, { status: 404 }) } // Build update object (only update provided fields) const updates: Record<string, string> = { updatedAt: new Date().toISOString(), } if (name) updates.name = name if (description !== undefined) updates.description = description if (digitRange) updates.digitRange = JSON.stringify(digitRange) if (regroupingConfig) updates.regroupingConfig = JSON.stringify(regroupingConfig) if (displayRules) updates.displayRules = JSON.stringify(displayRules) await db .update(customSkills) .set(updates) .where(and(eq(customSkills.id, id), eq(customSkills.userId, userId))) // Fetch updated skill const updated = await db.query.customSkills.findFirst({ where: and(eq(customSkills.id, id), eq(customSkills.userId, userId)), }) if (!updated) { return NextResponse.json({ error: 'Failed to fetch updated skill' }, { status: 500 }) } // Return parsed skill return NextResponse.json({ skill: { ...updated, digitRange: JSON.parse(updated.digitRange), regroupingConfig: JSON.parse(updated.regroupingConfig), displayRules: JSON.parse(updated.displayRules), }, }) } catch (error) { console.error('Failed to update custom skill:', error) return NextResponse.json({ error: 'Failed to update custom skill' }, { status: 500 }) } }) /** * DELETE /api/worksheets/skills/custom/[id] * * Delete a custom skill */ export const DELETE = withAuth(async (_request, { params }) => { try { const userId = await getUserId() const { id } = (await params) as { id: string } // Verify skill exists and belongs to user const existing = await db.query.customSkills.findFirst({ where: and(eq(customSkills.id, id), eq(customSkills.userId, userId)), }) if (!existing) { return NextResponse.json({ error: 'Custom skill not found' }, { status: 404 }) } // Delete the skill await db .delete(customSkills) .where(and(eq(customSkills.id, id), eq(customSkills.userId, userId))) return NextResponse.json({ success: true }) } catch (error) { console.error('Failed to delete custom skill:', error) return NextResponse.json({ error: 'Failed to delete custom skill' }, { status: 500 }) } }) |